package com.xuebank.nfe.nfecar.beas

import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.RelativeLayout
import com.wzn.libaray.utils.StatusBarUtil
import com.xuebank.nfe.nfecar.R
import com.xuebank.nfe.nfecar.beas.titlebar.NewTitleBar


/**
 * Created by Wind_Fantasy on 16/4/8.
 */
abstract class BaseTitleBarFragment : BaseFragment() {

    protected var titleBar: NewTitleBar? = null
        get() {
            if (activity !is BaseTitleBarActivity) {
                throw IllegalAccessException("getActivity() is not instance of BaseTitleBarActivity! ") as Throwable
            }
            field = (activity as BaseTitleBarActivity).titleBar
            return field
        }
    protected lateinit var mContentView: View
    protected lateinit var mContentRootView: FrameLayout

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initTitleBar()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        var view = onCreateViewMine(inflater, container, savedInstanceState)
        if (null != view)
            view = makeFinalContentView(view)
        return view
    }

    /**
     * 实现这个view来确保正确加载titlebar

     * @param inflater
     * *
     * @param container
     * *
     * @param savedInstanceState
     * *
     * @return
     */
    abstract fun onCreateViewMine(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?

    @JvmOverloads protected fun makeFinalContentView(contentView: View): View {
        mContentView = contentView
        if (null == contentView.background) {
            contentView.setBackgroundResource(R.color.main_background)
        }
        val relativeLayout = RelativeLayout(activity)

        val layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT)
        relativeLayout.layoutParams = layoutParams

        val contentRootView = FrameLayout(context)
        val contentRootViewLayoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        if (titleBar != null && titleBar!!.isTitleBarAbove) {
            contentRootViewLayoutParams.addRule(RelativeLayout.BELOW, titleBar!!.id)
        }
        relativeLayout.addView(contentRootView, contentRootViewLayoutParams)
        mContentRootView = contentRootView

        var contentViewLayoutParams: FrameLayout.LayoutParams? = contentView.layoutParams as FrameLayout.LayoutParams
        if (null == contentViewLayoutParams)
            contentViewLayoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        contentRootView.addView(contentView, contentViewLayoutParams)

        return relativeLayout
    }

    protected fun setStatusBarTransparent() {
        StatusBarUtil.setTranslucent(activity)
        if (titleBar != null) {
            val titleBarLayoutParams = titleBar!!.layoutParams as RelativeLayout.LayoutParams
            val layoutParams = mContentView.layoutParams as? RelativeLayout.LayoutParams
            if (layoutParams != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    layoutParams.removeRule(RelativeLayout.BELOW)
                } else {
                    layoutParams.addRule(RelativeLayout.BELOW, 0)
                }
            }
        }
    }

//    protected fun resetStatusBarTransparent() {
//        StatusBarUtil.resetSatusBar(activity)
//        if (titleBar != null) {
//            val titleBarLayoutParams = titleBar!!.layoutParams as RelativeLayout.LayoutParams
//            titleBarLayoutParams.topMargin -= StatusBarUtil.getStatusBarHeight(context)
//            val layoutParams = mContentView.layoutParams as? RelativeLayout.LayoutParams
//            layoutParams?.addRule(RelativeLayout.BELOW, titleBar!!.id)
//        }
//    }

//    protected fun initTitleBar() {
//        val fragmentManager = activity.supportFragmentManager
//        val count = fragmentManager.backStackEntryCount
//        if (count > 0) {
//            //            setLeftTextViewDrawableLeft(R.drawable.left_arrow)
//            //                    .setLeftTextView("", new View.OnClickListener() {
//            //                        @Override
//            //                        public void onClick(View v) {
//            //                            onBackPressed();
//            //                        }
//            //                    });
//        }
//
//    }

    /**
     * 实现时需注意，需要先reset
     */
    abstract fun initTitleBar()

    override fun getContentRootView(): FrameLayout? {
        return mContentRootView
    }
}
